package cn.kais.drawer;

import cn.kais.drawer.bean.BeanBoard;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DrawGenerater {

    /**
     * 测试用  获取本地Json 模板内容
     */
    public static JSONObject doTestGetJson(String name) throws IOException {
        String mPath = Commons.JSON_PATH + name + ".json";
        String mContent = new String(Files.readAllBytes(Paths.get(mPath)));
        return JSONObject.parseObject(mContent);
    }

    /**
     * 测试用 创建价签模板
     */
    public static void doTestCreatePrice(JSONObject content) {
        JSONArray views = content.getJSONArray("pages");
        for (int i = 0; i < views.size(); i++) {
            content.remove("pages");
            JSONObject current = views.getJSONObject(i);
            content.put("current", current);
            //
            BufferedImage view = DrawGenerater.onCreatePriceBuffer(content);
            //
            String name = current.getString("title");
            UnitTools.doTestWrite(view, name);
        }
    }

    /**
     * 测试用 创建某个状态的价签模板
     *
     * @param state 状态：1.原价 2.促销价 3.会员价 4.会员特价 5.其他价格
     */
    public static BufferedImage doTestCreatePriceForState(JSONObject content, String state) {
        JSONArray views = content.getJSONArray("pages");
        List<JSONObject> list = views.toJavaList(JSONObject.class);
        List<JSONObject> items = list.stream().filter(item -> item.getString("id").equals(state)).collect(Collectors.toList());

        if (items.isEmpty()) {
            System.out.println("为获取到指定状态价签模板信息");
            return null;
        }
        JSONObject current = items.get(0);
        content.remove("pages");
        content.put("current", current);
        //
        BufferedImage view = DrawGenerater.onCreatePriceBuffer(content);
        //
        String name = current.getString("title");
        UnitTools.doTestWrite(view, name);
        return view;
    }

    /**
     * 测试用 创建营销模板
     */
    public static void doTestCreateMarket(JSONObject content) throws IOException {
        JSONArray views = content.getJSONArray("pages");
        for (int i = 0; i < views.size(); i++) {
            content.remove("pages");
            JSONObject current = views.getJSONObject(i);
            Map<String, BufferedImage> bis = new HashMap<>();
            /*
             * 1.开启 则动态绘制 营销模板内部的价签模板组件
             * 2.关闭 则静态绘制 使用营销模板内部使用的静态价签图片地址
             */
//            List<String> ids = doGetPriceIdsForMarket(current);
//            for (int j = 0; j < ids.size(); j++) {
//                String id = ids.get(j);
//                JSONObject mPriceJson = DrawGenerater.doTestGetJson("price");
//                BufferedImage bi = doTestCreatePriceForState(mPriceJson, String.valueOf(j + 1));
//                bis.put(id, bi);
//            }
            content.put("current", current);
            //
            BufferedImage view = DrawGenerater.onCreateMarketBuffer(content, bis);
            //
            String name = current.getString("title");
            UnitTools.doTestWrite(view, name);
        }
    }

    /**
     * 对外
     * Json 生成价签模板图片
     *
     * @param content Json 串
     * @return 图片 buffer
     */
    public static BufferedImage onCreatePriceBuffer(JSONObject content) {
//        System.out.println(content.toJSONString());
        BeanBoard bean = new BeanBoard(content);
        return bean.view;
    }

    /**
     * 对外
     * Json 生成价签模板图片
     *
     * @param content Json 串
     * @return Stream
     */
    public static ByteArrayOutputStream onCreatePriceStream(JSONObject content) {
        BeanBoard bean = new BeanBoard(content);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            ImageIO.write(bean.view, "png", stream);
            return stream;
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 对外
     * Json 生成价营销模板图片
     *
     * @param content Json 串
     */
    public static BufferedImage onCreateMarketBuffer(JSONObject content, Map<String, BufferedImage> bis) {
        System.out.println(content.toJSONString());
        BeanBoard bean = new BeanBoard(content, bis);
        return bean.view;
    }

    /**
     * 对外
     * Json 生成价营销模板图片
     *
     * @param content Json 串
     */
    public static ByteArrayOutputStream onCreateMarketStream(JSONObject content, Map<String, BufferedImage> bis) {
        System.out.println(content.toJSONString());
        BeanBoard bean = new BeanBoard(content, bis);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            ImageIO.write(bean.view, "png", stream);
            return stream;
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 对外
     * 获取营销模板中 价签类型类型 小组件  价签id列表
     *
     * @param current 1品 2品 类营销模板Json
     */
    public static List<String> doGetPriceIdsForMarket(JSONObject current) {
        List<String> ids = new ArrayList<>();
        JSONArray coms = current.getJSONArray("componentList");
        for (int i = 0; i < coms.size(); i++) {
            JSONObject com = coms.getJSONObject(i);
            String type = com.getString("type");
            if (type.equals("TumengPriceTag")) {
                String id = doGetPriceIdForMarketCom(com);
                if (id != null) {
                    ids.add(id);
                }
            }
        }
        return ids;
    }

    /**
     * 对外
     * 获取营销模板中的价签组件 的价签模板id
     *
     * @param com 营销模板中的价签组件
     * @return 价签模板id
     */
    public static String doGetPriceIdForMarketCom(JSONObject com) {
        System.out.println(com.toJSONString());
        JSONArray tas = com.getJSONArray("priceTagList");
        if (!tas.isEmpty()) {
            JSONObject ta = tas.getJSONObject(0);
            return ta.getString("priceTagId");
        }
        return null;
    }

}
